/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework.writers
{
	import funit.framework.Assert;
	import funit.framework.MessageString;
	
	import mx.collections.ArrayCollection;
	
	[TestFixture]
	public class MessageWriterTests
	{
		private var writer:MessageWriter;
		
		
		public function MessageWriterTests()
		{
			
		}
		
		
		[SetUp]
		public function SetUp() : void
		{
			writer = new MessageWriter();
		}
		
		[Test]
		public function writeConnector() : void
		{
			writer.writeConnector("and");
			Assert.areEqual(" and ", writer.toString());
		}
		
		[Test]
		public function writePrepend() : void
		{
			writer.writePrepend("contains");
			Assert.areEqual("contains ", writer.toString());
		}
		
		[Test]
		public function writeInteger() : void
		{
			writer.writeValue(42);
			Assert.areEqual("42", writer.toString());
		}
		
		[Test]
		public function writeString() : void
		{
			writer.writeValue("Value");
			Assert.areEqual("\"Value\"", writer.toString());
		}
		
		[Test]
		public function writeStringEscapeCharacters() : void
		{
			writer.writeValue("Yours Truly,\r\n\tMark\r\n");
			Assert.areEqual("\"Yours Truly,\\r\\n\\tMark\\r\\n\"", writer.toString());
		}
		
		[Test]
		public function writeNumberTrimToSixteenDigits() : void
		{
			writer.writeValue(0.333333333333333333333333333333);
			Assert.areEqual("0.3333333333333333", writer.toString());
			Assert.areEqual(18, writer.toString().length); // add 2 for leading 0 and decimal
		}
		
		[Test]
		public function writeNumberScientificNotation() : void
		{
			writer.writeValue(12345678901234567890123456789);
			Assert.areEqual("1.23456789012345e+28", writer.toString());
		}
		
		[Test]
		public function writeDate() : void
		{
			writer.writeValue(new Date(2007, 6, 4, 9, 15, 30));
			Assert.areEqual("2007-07-04 09:15:30", writer.toString());
		}
		
		[Test]
		public function writeDateMilliseconds() : void
		{
			writer.writeValue(new Date(2007, 6, 4, 9, 15, 30, 12));
			Assert.areEqual("2007-07-04 09:15:30.012", writer.toString());
		}
		
		[Test]
		public function writeArray() : void
		{
			var al:Array = new Array( "x", "y", new Object() );
			
			writer.writeValue(al);
			Assert.areEqual("[ \"x\", \"y\", <Object> ]", writer.toString());
		}
		
		[Test]
		public function writeArrayTrimmed() : void
		{
			var a1:Array = new Array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m" );
			var expected:String = "[ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"... ]";
			
			writer.writeValue(a1);
			Assert.areEqual(expected, writer.toString());
		}
		
		[Test]
		public function writeCollection() : void
		{
			var al:ArrayCollection = new ArrayCollection();
			
			al.addItem("x");
			al.addItem("y");
			al.addItem(new Object());
			
			writer.writeValue(al);
			Assert.areEqual("[ \"x\", \"y\", <Object> ]", writer.toString());
		}
		
		[Test]
		public function writeCollectionTrimmed() : void
		{
			var a1:ArrayCollection = new ArrayCollection( ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"] );
			var expected:String = "[ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"... ]";
			
			writer.writeValue(a1);
			Assert.areEqual(expected, writer.toString());
		}
		
		[Test]
		public function displayStringDifferencesTrimmed() : void
		{
			var value:String = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
			var expected:String = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXY...";
			
			writer.displayStringDifferences(value, "abcde", 5, false, true);
			Assert.areEqual(
				MessageString.ExpectedPrefix + wrapString(expected) + MessageString.NewLine +
					MessageString.ActualPrefix + wrapString("abcde") + MessageString.NewLine +
					"  ----------------^" + MessageString.NewLine,
				writer.toString()
			);
		}
		
		[Test]
		public function displayStringDifferencesNoTrim() : void
		{
			var value:String = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
			
			writer.displayStringDifferences(value, "abcde", 5, false, false);
			Assert.areEqual(
				MessageString.ExpectedPrefix + wrapString(value) + MessageString.NewLine +
					MessageString.ActualPrefix + wrapString("abcde") + MessageString.NewLine +
					"  ----------------^" + MessageString.NewLine,
				writer.toString()
			);
		}
		
		private function wrapString( value:String ) : String
		{
			return "\"" + value + "\"";
		}
		
	}
	
}
